package ltd.smallwang.infrastructure.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author justd
 * @description 功能描述
 * @since 2024-04-26 00:38
 **/
@Configuration
public class WebMvcConfig {

  @NacosValue("${allowedOrigins}")
  private String allowedOrigins;

  @Bean
  public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
      @Override
      public void addCorsMappings(CorsRegistry registry) {
        String[] allowedOriginArr = allowedOrigins.split(",");
        registry.addMapping("/**").allowedOrigins(allowedOriginArr);
      }
    };
  }
}
